package com.sjy.ship.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.sjy.common.constant.CacheConstants;
import com.sjy.common.core.domain.AjaxResult;
import com.sjy.common.core.redis.RedisCache;
import com.sjy.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sjy.ship.mapper.ShipInfoMapper;
import com.sjy.ship.domain.ShipInfo;
import com.sjy.ship.service.IShipInfoService;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

/**
 * 船舶信息Service业务层处理
 * 
 * @author lzm
 * @date 2023-06-14
 */
@Service
public class ShipInfoServiceImpl implements IShipInfoService 
{
    @Autowired
    private ShipInfoMapper shipInfoMapper;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询船舶信息
     * 
     * @param shipId 船舶信息主键
     * @return 船舶信息
     */
    @Override
    public ShipInfo selectShipInfoByShipId(Long shipId)
    {
        return shipInfoMapper.selectShipInfoByShipId(shipId);
    }

    /**
     * 查询船舶信息列表
     * 
     * @param shipInfo 船舶信息
     * @return 船舶信息
     */
    @Override
    public List<ShipInfo> selectShipInfoList(ShipInfo shipInfo)
    {
        return shipInfoMapper.selectShipInfoList(shipInfo);
    }

    /**
     * 新增船舶信息
     * 
     * @param shipInfo 船舶信息
     * @return 结果
     */
    @Override
    public AjaxResult insertShipInfo(ShipInfo shipInfo)
    {
        shipInfo.setCreateTime(DateUtils.getNowDate());
        Assert.isTrue(shipInfoMapper.insertShipInfo(shipInfo)>0,"操作失败,请稍后重试");
        selectShipInfoCacheList(true);
        return AjaxResult.success();
    }

    /**
     * 修改船舶信息
     * 
     * @param shipInfo 船舶信息
     * @return 结果
     */
    @Override
    public AjaxResult updateShipInfo(ShipInfo shipInfo)
    {
        shipInfo.setUpdateTime(DateUtils.getNowDate());
        Assert.isTrue(shipInfoMapper.updateShipInfo(shipInfo)>0,"操作失败,请稍后重试");
        selectShipInfoCacheList(true);
        return AjaxResult.success();
    }

    /**
     * 批量删除船舶信息
     * 
     * @param shipIds 需要删除的船舶信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteShipInfoByShipIds(Long[] shipIds)
    {
        Assert.isTrue(shipInfoMapper.deleteShipInfoByShipIds(shipIds)>0,"操作失败,请稍后重试");
        selectShipInfoCacheList(true);
        return AjaxResult.success();
    }

    /**
     * 删除船舶信息信息
     * 
     * @param shipId 船舶信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteShipInfoByShipId(Long shipId)
    {
        Assert.isTrue(shipInfoMapper.deleteShipInfoByShipId(shipId)>0,"操作失败,请稍后重试");
        selectShipInfoCacheList(true);
        return AjaxResult.success();
    }

    @Override
    public List<ShipInfo> selectShipInfoCacheList(boolean isDelete) {
        //先获取缓存数据
        List<ShipInfo> cacheList = redisCache.getCacheList(CacheConstants.SHIP_INFO_LIST);
        boolean noCache =CollectionUtils.isEmpty(cacheList);
        //如果要删除且存在，则删除
        if (isDelete && !noCache){
            redisCache.deleteObject(CacheConstants.SHIP_INFO_LIST);
            cacheList=null;
        }

        if (noCache){
            cacheList=shipInfoMapper.selectShipInfoList(new ShipInfo());
            redisCache.setCacheList(CacheConstants.SHIP_INFO_LIST,cacheList);
        }
        return cacheList;
    }
}
